﻿#define SVN_EXIST
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

//Automation command line from: http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-automation.html#tsvn-automation-basics

namespace TUsvnAddIn
{
    partial class Connect
    {
        #region Delegate
        private delegate string CommandExecFunctionDelegate(string file);
        #endregion

        #region private
        private const string SVN_EXECUTION = "TortoiseProc.exe";

        //" /closeonend:0" don't close the dialog automatically 
        //" /closeonend:1" auto close if no errors
        //" /closeonend:2" auto close if no errors and conflicts 
        //" /closeonend:3" auto close if no errors, conflicts and merges
        //" /closeonend:4" auto close if no errors, conflicts and merges for local operations
                                     
        private const string AUTO_CLOSE_COMMAND = " /closeonend:0"; //try using closeonend:3 for closing the window for no errors:
        #endregion

        private static void execCommand(CommandExecFunctionDelegate cmdFunction, string file)
        {
            try
            {
                if(cmdFunction != null)
                {
                    string argument = cmdFunction.Invoke('"' + file + '"');
                    ProcessStartInfo processInfo = new ProcessStartInfo(SVN_EXECUTION, argument);
                    Process proc = new Process();
                    proc.StartInfo = processInfo;
#if SVN_EXIST
                    proc.Start();
#else
                    message(argument);
#endif
                }
            }
            catch(Exception ex)
            {
                message(ex.Message);
            }
        }

        #region TortoiseSVN command functions

        private static string cmdCommit(string file)
        {
            return ("/command:commit /path:" + file + AUTO_CLOSE_COMMAND);
        }

        private static string cmdRevert(string file)
        {
            return ("/command:revert /path:" + file + AUTO_CLOSE_COMMAND);
        }

        private static string cmdAdd(string file)
        {
            return ("/command:add /path:" + file + AUTO_CLOSE_COMMAND);
        }

        private static string cmdDiff(string file)
        {
            return ("/command:diff /path:" + file + AUTO_CLOSE_COMMAND);
        }

        private static string cmdRevisionGraph(string file)
        {
            return ("/command:revisiongraph /path:" + file + AUTO_CLOSE_COMMAND);
        }

        private static string cmdUpdate(string file)
        {
            return ("/command:update /path:" + file + AUTO_CLOSE_COMMAND);
        }

        private static string cmdReproBrowser(string url)
        {
            return ("/command:repobrowser /path:" + url + AUTO_CLOSE_COMMAND);
        }

        private static string cmdShowLog(string file)
        {
            return ("/command:log /path:" + file + AUTO_CLOSE_COMMAND);
        }

        private static string cmdCheckForModification(string url)
        {
            return ("/command:repostatus /path:" + url + AUTO_CLOSE_COMMAND);
        }

        private static string cmdCleanUp(string url)
        {
            return ("/command:cleanup /path:" + url + AUTO_CLOSE_COMMAND);
        }
        #endregion
    }
}

